/* $Id: SubmissionsFormatter.java 110 2011-01-24 21:33:40Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.gui;

import alt.djudge.frontend.server.datatypes.ContestProblemEntry;
import alt.djudge.frontend.server.datatypes.ContestUserEntry;
import alt.djudge.frontend.server.datatypes.scores.ContestUserScoreEntry;
import alt.djudge.frontend.server.datatypes.scores.ProblemUserScoreEntry;
import alt.djudge.frontend.server.models.BasicMonitor;


@SuppressWarnings("unused")
public class MonitorFormatter extends AbstractFormatter<ContestUserScoreEntry>
{
	//private Map<Long, ContestProblemEntry> problems = ContestProblemsModel.getEntriesMap();
	
	//private Map<Long, UserEntry> users = UsersModel.getEntriesMap();
	
//	private Map<Long, ContestUserEntry> contestUsers;
	
	private String trClass = null;//"b1";
	
	private String tdClass = null;//"b1";
	
	private String thClass = null;//"b1";
	
	private BasicMonitor monitor;
	
	ContestUserScoreEntry prev = null;

	int c = 0, last_score = -1, last_place = 0;
	long last_time = -1;

	
	public MonitorFormatter()
	{
		
	}
	
	public String format(BasicMonitor monitor)
	{
		//contestUsers = ContestUsersModel.getEntriesMap(monitor.getContestId());
		this.monitor = monitor;
		
		startTable(getTableClass(), getTableWidth(), getTableStyle(), getTableBorder());

		// headers
		addHeaders();
		
		c = 0;
		last_score = -1;
		last_place = 0;
		last_time = -1;
		
		// adding rows
		for (ContestUserEntry contestUserEntry : monitor.getUsers())
		{
			addRow(monitor.getUserScore(contestUserEntry.getUserId()));
			prev = monitor.getUserScore(contestUserEntry.getUserId());
		}

		// finish table
		endTable();
		
		return getResult();
	}
	
	@Override
	protected void addClassData(ContestUserScoreEntry entry)
	{
		c++;
		if (entry.getTotalScore() != last_score)
			last_place = c;
		last_score = entry.getTotalScore();
		
		addTableData(last_score > 0 ? "" + last_place : "*", "st_place", null);
		
		ContestUserEntry user = monitor.getUser(entry.getUserId());
		
		addTableData(user.getName(), "st_team", null);
		
		int totalScore = 0;
		for (ContestProblemEntry problemEntry: monitor.getProblems())
		{
			ProblemUserScoreEntry score = monitor.getProblemUserScore(problemEntry.getId(), entry.getUserId());

			String txt = "-";
			if (score != null && score.getMaxScore() >= 0)
			{
				totalScore += score.getMaxScore();
				if (score.getAccepted())
					txt = "<b>" + score.getMaxScore() + "</b><div class=\"st_time\">(" + score.getMaxScoreFirstTimeStr() + ")</div>";
				else
					txt = "" + score.getMaxScore() + " <div class=\"st_time\">(" + score.getMaxScoreFirstTimeStr() + ")</div>";
			}
			String color = "";
			if (score != null)
			{
				color = (score.getAccepted() ? colorAC : 
								(score.getPendingCount() > 0 ? colorPending :
									(score.getUniqueRejectedCount() > 0 ? colorWA : "")
								)
							);
			}
			addTableData(txt, "st_prob", color.length() > 0 ? "background-color: " + color + "; " : null);
		}
		
		addTableHeader("" + totalScore, "st_score");
	}

	@Override
	protected void addClassHeaders()
	{
		addTableHeader("Місце", "st_place");
		addTableHeader("Учасник", "st_team");
		
		for (ContestProblemEntry problemEntry: monitor.getProblems())
			addTableHeader(problemEntry.getSid(), "st_prob");
		
		addTableHeader("Сума балів", "st_score");
	}

	@Override
	protected String getTableClass()
	{
		return "standings";
	}
	
	@Override
	protected String getCustomRowClass(ContestUserScoreEntry entry)
	{
		return null;
	}
}
